package com.qf.common.core.utils;

import com.qf.common.core.constant.CommonConstant;
import com.qf.common.core.entity.User;
import com.qf.common.core.enums.RedisKeyEnum;
import com.qf.common.redis.utils.RedisUtil;
import io.jsonwebtoken.Claims;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * --- 缘分让我们相遇乱世以外 ---
 *
 * @author SJY
 * 2024/12/26
 */
// @Component
public class TokenUtils {

    // @Resource
    // private RedisUtil redisUtil;

    /**
     * 通过token获取用户id
     */
    public static Integer getUid() {
        // 动态获取HttpServletRequest对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader(CommonConstant.TOKEN);
        // // 从spring容器中获取redisUtil对象
        // RedisUtil redisUtil = SpringBeanUtils.getBean(RedisUtil.class);
        // User user = redisUtil.getCacheObject(String.format(RedisKeyEnum.KEY_LOGIN.getKey(), token));
        // return user.getId();

        Claims claims = JwtUtils.parseJWT(token);
        return (Integer) claims.get("uid");
    }
}
